package cn.e4j.modular.crm.controller.base;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwbcT;
import cn.e4j.modular.crm.entity.GwbclT;
import cn.e4j.modular.crm.service.GwbcTService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 关键项评估维护 控制层
 * @author mengyb
 * @date 2019-04-10
 */
@Controller
@RequestMapping("/base/gwat234")
public class Gwat234Controller extends BaseController {

    @Autowired
    private GwbcTService gwbcTService;

    /**
     * 跳转至关键项评估维护主界面
     *
     * @return
     */
    @RequiresPermissions("base:gwat234:init")
    @RequestMapping(value = {"", "/init"})
    public String index() {
        return "/pc/crm/base/gwat234.html";
    }

    @RequiresPermissions("base:gwat234:init")
    @RequestMapping("/getList")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {

        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();

        List<Map<String, Object>> list = gwbcTService.getList(inDto, page);

        return new PageReData(page, list);

    }

    @RequiresPermissions("base:gwat234:init")
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(GwbcT gwbcT, GwbclT gwbclT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbcT.setGWBCENT(shiroUser.getDBC());
        if (gwbcTService.existedTheParams(gwbcT)) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        } else {
            gwbcTService.add(gwbcT, gwbclT);
            return SUCCESS_TIP;
        }
    }

    @RequiresPermissions("base:gwat234:init")
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData edit(GwbcT gwbcT, GwbclT gwbclT) {
        gwbcTService.edit(gwbcT, gwbclT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("base:gwat234:init")
    @RequestMapping("/delRow")
    @ResponseBody
    public ResponseData delRow(GwbcT gwbcT, GwbclT gwbclT) {
        gwbcTService.delRow(gwbcT, gwbclT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("base:gwat234:init")
    @RequestMapping("/delRows")
    @ResponseBody
    public ResponseData delRows(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbcT> gwbcTList = inDto.getReqList(GwbcT.class);
        gwbcTService.delRows(gwbcTList);
        return SUCCESS_TIP;
    }


}
